package com.android.SAXParser;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ChannelActivity extends ListActivity {
	
	ArrayList<String> dagskrain;
	XMLGettersSetters data;
	MySQLiteHelper SQLhelp;
//	ArrayList<String> about = new ArrayList<String>();
	ArrayList<String> name = new ArrayList<String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		List<Reminder> dagskra = new ArrayList<Reminder>();
		SQLhelp = new MySQLiteHelper(this);

		Bundle extras = getIntent().getExtras();

		dagskrain = extras.getStringArrayList("dagskra");
	//	about = extras.getStringArrayList("about");
		String Link = extras.getString("Linkurinn");
		String Date = extras.getString("date");
		
		try {
			SAXParserFactory saxPF = SAXParserFactory.newInstance();
			SAXParser saxP = saxPF.newSAXParser();
			XMLReader xmlR = saxP.getXMLReader();

			URL url = new URL("http://www.vodafone.is" + Link);
			Log.d("urlid",url.toString());

			XMLHandler_channel myXMLHandler = new XMLHandler_channel();
			xmlR.setContentHandler(myXMLHandler);
			xmlR.parse(new InputSource(url.openStream()));
			}
			catch (Exception e) {
				System.out.println(e);
			}
			data = XMLHandler_channel.data;
			
			for (int i = 0; i < data.getTitle().size(); i++) {
				
				if (data.getDate().get(i).equals(Date)) 
				{
			//		Log.d("getTitleSize",String.valueOf(data.getTitle().size()));
					
				//	dagskra.add(data.getTime().get(i) + "  "+ data.getTitle().get(i));
					dagskra.add(get(data.getTitle().get(i) ,data.getTime().get(i)));
					//about.add(data.getDescription().get(i));
			//		Log.i("datedate", data.getDate().get(i));
			//		Log.i("datedatetitle", data.getTitle().get(i));
				}
			}

		ArrayAdapter<Reminder> adapter = new ChannelAdapter(this, dagskra, SQLhelp);
		setListAdapter(adapter);

		ListView listView = getListView();
		listView.setTextFilterEnabled(true);
	}

	private Reminder get(String title,String time) {
		return new Reminder(title,time);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
//	//	String aboutTheProgram = about.get((int) id);
//		String nafn = dagskrain.get((int) id);
//		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
//		alertDialog.setTitle(nafn);
//		//alertDialog.setMessage(aboutTheProgram);
//		alertDialog.show();
//		alertDialog.setCanceledOnTouchOutside(true);

	}
	@Override
	  public boolean onCreateOptionsMenu(Menu menu) {
	   getMenuInflater().inflate(R.menu.channel_list_view, menu);
	   return true;
	  }

	  public boolean onOptionsItemSelected(MenuItem item) {
	   switch (item.getItemId()) {

	   case R.id.mychannels: {
	    Intent intent = new Intent(this, MyChannels.class);
	    startActivity(intent);
	    return true;
	   }
	   
	   case R.id.My_Reminders: {
		    Intent intent = new Intent(this, ReminderList.class);
		    startActivity(intent);
		    return true;
	   }

	   default:
	    return super.onOptionsItemSelected(item);
	   }
	  }


}
